using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using ZedGraph;

namespace ICP_MS.Plugin.Experiment;

public class LabelGraph : ZedGraphControl
{
	public class MassLabel
	{
		public string label { get; set; }

		public double mass { get; set; }

		public double Intensity { get; set; }

		public double realmass { get; set; }

		public string tag { get; set; }

		public Color color { get; set; }

		public MassLabel(string lbl, double m, double intensity, double rm, string tg, Color cor)
		{
			label = lbl;
			mass = m;
			Intensity = intensity;
			realmass = rm;
			tag = tg;
			color = cor;
		}
	}

	public bool IsShowLabel = true;

	public List<MassLabel> masslabels = new List<MassLabel>();

	public void AddMassLabel(string label, double m, double intensity, double rm, string tag, Color color)
	{
		masslabels.Add(new MassLabel(label, m, intensity, rm, tag, color));
	}

	public PointF ChangeRealtoPos(PointF pointReal)
	{
		return base.GraphPane.GeneralTransform(pointReal, CoordType.AxisXYScale);
	}

	public void RemoveAllLabels()
	{
		masslabels.Clear();
	}

	public void RemoveLabel(string tag)
	{
		List<int> labels = new List<int>();
		for (int i = 0; i < masslabels.Count; i++)
		{
			if (masslabels[i].tag == tag)
			{
				labels.Add(i);
			}
		}
		if (labels.Count > 0)
		{
			masslabels.RemoveRange(labels[0], labels.Count);
		}
	}

	public void ChangeLabel(string tag, Color color)
	{
		for (int i = 0; i < masslabels.Count; i++)
		{
			if (masslabels[i].tag == tag)
			{
				masslabels[i].color = color;
			}
		}
	}

	public void RemoveLabel(List<string> tags)
	{
		foreach (MassLabel label in masslabels)
		{
			if (tags.Contains(label.tag))
			{
				masslabels.Remove(label);
			}
		}
	}

	protected override void OnPaint(PaintEventArgs e)
	{
		base.OnPaint(e);
		if (!IsShowLabel)
		{
			return;
		}
		float xmin = base.GraphPane.Chart.Rect.X;
		float ymin = base.GraphPane.Chart.Rect.Y;
		float width = base.GraphPane.Chart.Rect.Width;
		float height = base.GraphPane.Chart.Rect.Height;
		foreach (MassLabel masslabel in masslabels)
		{
			PointF pointreal = new PointF((float)masslabel.realmass, (float)masslabel.Intensity);
			PointF pointpos = ChangeRealtoPos(pointreal);
			if (pointpos.X > xmin && pointpos.X < xmin + width && pointpos.Y > ymin && pointpos.Y < ymin + height)
			{
				Brush brush = new SolidBrush(masslabel.color);
				Font font = new Font("Arial", 9f, FontStyle.Bold);
				e.Graphics.DrawString(masslabel.label, font, brush, pointpos);
			}
		}
	}

	private void InitializeComponent()
	{
		base.SuspendLayout();
		base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 12f);
		base.Name = "LabelGraph";
		base.Size = new System.Drawing.Size(592, 326);
		base.ResumeLayout(false);
	}
}
